package com.cg.asm.body.controller.org;

import com.alibaba.fastjson.JSONObject;
import com.cg.asm.body.config.loading.cache.org.IOrgCache;
import com.cg.asm.body.domain.dto.org.OrgRegDTO;
import com.cg.asm.body.domain.vo.org.OrgUpdateStatus;
import com.cg.asm.body.service.org.IOrgService;
import com.cg.asm.body.util.constants.DicEnum;
import com.cg.asm.body.util.constants.EnumGroup;
import com.cg.asm.client.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@Api(tags = "OrgController", value = "组织机构相关结构")
@RestController
@RequestMapping("/org")
public class OrgController {

    private IOrgService orgService;
    private IOrgCache orgCache;

    public OrgController(IOrgService orgService, IOrgCache orgCache) {
        this.orgService = orgService;
        this.orgCache = orgCache;
    }

    @ApiOperation(value = "获取所有在用的组织机构树信息[缓存数据]", notes = "组织机构")
    @GetMapping("/treeData")
    public JSONObject treeData() {
        return CommonUtil.successJson(orgCache.getCacheOrgTreeData());
    }

    @ApiOperation(value = "获取组织机构树信息", notes = "使用场景：组织机构管理页面")
    @GetMapping("/orgTreeData")
    public JSONObject orgTreeData() {
        return CommonUtil.successJson(orgService.getOrgTreeDataFullCol());
    }

    @ApiOperation(value = "根据组织机构编码获取所有需展开节点编码", notes = "组织机构")
    @ApiImplicitParam(name = "orgCode", value = "组织机构编码", required = true, dataType = "String", paramType = "path")
    @GetMapping("/expandedCodes")
    public JSONObject expandedCodes(String orgCode) {
        return CommonUtil.successJson(orgService.expandedCodes(orgCode));
    }

    @ApiOperation(value = "获取系统内置组织机构状态", notes = "组织机构")
    @GetMapping("/orgStatusOptions")
    public JSONObject orgStatusOptions() {
        return CommonUtil.successJson(DicEnum.getEnum(EnumGroup.STATUS_GROUP));
    }

    @ApiOperation(value = "保存组织机构信息", notes = "组织机构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgRegDTO", value = "组织机构注册对象", required = true, dataType = "OrgRegDTO", paramType = "body")
    })
    @PostMapping("/saveOrg")
    public JSONObject saveOrg(OrgRegDTO orgRegDTO) {
        try {
            orgService.saveOrg(orgRegDTO);
            orgCache.initOrgTreeData();
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "删除组织机构", notes = "组织机构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgCode", value = "组织机构编码", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/delOrg")
    public JSONObject delOrg(@RequestBody String orgCode) {
        try {
            orgService.delOrg(orgCode);
            orgCache.initOrgTreeData();
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "更新组织机构信息", notes = "组织机构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgRegDTO", value = "组织机构注册对象", required = true, dataType = "OrgRegDTO", paramType = "body")
    })
    @PostMapping("/saveEditOrg")
    public JSONObject saveEditOrg(OrgRegDTO orgRegDTO) {
        try {
            orgService.saveEditOrg(orgRegDTO);
            orgCache.initOrgTreeData();
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "批量更新组织机构状态", notes = "组织机构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgUpdateStatus", value = "组织机构状态更新对象", required = true, dataType = "OrgUpdateStatus", paramType = "body")
    })
    @PutMapping("/updateStatusBatch")
    public JSONObject updateStatusBatch(@RequestBody OrgUpdateStatus orgUpdateStatus) {
        try {
            orgService.updateStatusBatch(orgUpdateStatus);
            orgCache.initOrgTreeData();
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }
}
